package pl.com.like.sequoia.maintenance.importer;

import java.io.File;
import java.text.MessageFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;

import pl.com.like.sequoia.biojava.pdb.IPdbService;
import pl.com.like.sequoia.biojava.util.ILocalFileImporter;
import pl.com.like.sequoia.dao.admin.IUploadedFileDao;
import pl.com.like.sequoia.exceptions.LogicalException;
import pl.com.like.sequoia.model.admin.UploadedFile;
import pl.com.like.sequoia.model.bio.BioObjectHandler;
import pl.com.sequoia.jms.helper.IMessageObjectHandler;

/**
 * 
 * @author pwolanski
 * 
 */
public class PdbFileScanner implements IMessageObjectHandler {

	private final Logger log = LoggerFactory.getLogger(PdbFileScanner.class);

	@Autowired
	private IUploadedFileDao uploadedFileDao;
	@Autowired
	private ILocalFileImporter localFileImporter;
	@Autowired
	private IPdbService pdbService;

	private UploadedFile failedScan(UploadedFile uploadedFile, String msg) {
		Assert.notNull(uploadedFile);
		uploadedFile.setScanResult(Boolean.FALSE);
		if (msg == null) {
			uploadedFile.setScanInfo("Could not get uploaded file from disc.");
		} else {
			uploadedFile.setScanInfo(msg);
		}
		return uploadedFile;
	}

	public IPdbService getPdbService() {
		return pdbService;
	}

	public void proceed(Long objectId) {
		Assert.notNull(objectId);
		UploadedFile uploadedFile = uploadedFileDao.get(objectId);
		if (uploadedFile == null) {
			log.warn("Trying to get UploadedFile object with id = {}, but return null.", objectId);
		}
		String filePath = uploadedFile.getPath();
		if (!filePath.isEmpty()) {
			File uplFile = new File(filePath);
			if (uplFile.exists()) {
				BioObjectHandler objectHandler = null;
				try {
					objectHandler = localFileImporter.setObject(uplFile);
					log.info("uploaded file has been saved as bioObjectHandler object with id {}", objectHandler
							.getId());
					uploadedFile.setScanResult(Boolean.TRUE);
					pdbService.completeOptionals(objectHandler);
					uplFile.delete();
					log.info("file have been scanned successfully. Now it is time to delete it from upload dir {}",
							filePath);
				} catch (LogicalException e) {
					String msg = MessageFormat.format("given file {0} is not valid PDB file", filePath);
					log.warn(msg, e);
					uploadedFile = failedScan(uploadedFile, e.getMessage());
				}
			} else {
				uploadedFile = failedScan(uploadedFile, "file have been deleted from filesystem");
			}
		} else {
			uploadedFile = failedScan(uploadedFile, "incorrect filepath");
		}
		Date currentDate = Calendar.getInstance(TimeZone.getDefault()).getTime();
		uploadedFile.setScanningDate(currentDate);
		uploadedFileDao.saveOrUpdate(uploadedFile);

	}

	public void setPdbService(IPdbService pdbService) {
		this.pdbService = pdbService;
	}

}